package com.mq.study.cosumer.compent;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class RabbitReceive {

    @RabbitListener(bindings = @QueueBinding(
                    value = @Queue(value = "queue-1",durable = "true"),
                    exchange = @Exchange(name = "exchange-1",durable = "true",type = "topic"
                    ,ignoreDeclarationExceptions = "true"),
                    key = "springboot.*"
           )
    )
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws IOException {
        //1、收到消息以后进行业务端消费处理
        System.out.println("-------------------------");
        System.out.println("消费消息-------------------------"+message.getPayload());

        //2、处理成功之后获取deliveryTag并进行手工的ACK操作，因为我们配置文件里配置的是手工签收
        //spring.rabbitmq.listener.simple.acknowledge-mode=manual
        Long delliveryTag=(Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(delliveryTag,false);
    }

}
